//
//  EFSelectCollectionMenuCell.m
//  ESPTablet
//
//  Created by Javor on 14-8-12.
//  Copyright (c) 2014年 Javor. All rights reserved.
//

#import "EFSelectCollectionMenuCell.h"
#ifdef NEED_FRAMEWORK
#import <IOSESPBaseFramework/FileUtil.h>
#else
#import "FileUtil.h"
#endif
#import "EnvironmentVariable.h"

@interface EFSelectCollectionMenuCell(){
    UIImageView *_icon;
    UILabel *_label;
    SSCheckBoxView *_checkBox;
}

@end

@implementation EFSelectCollectionMenuCell
@synthesize imagePath = _imagePath, title = _title;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _icon = [[UIImageView alloc] init];
        
        _label = [[UILabel alloc] init];
        _label.textAlignment = NSTextAlignmentCenter;
        
        _isChecked = NO;
        _checkBox = [[SSCheckBoxView alloc] initWithFrame:CGRectMake(0, 0, 30, 30) style:kSSCheckBoxViewStyleMono checked:_isChecked];
        
        [self.contentView addSubview:_icon];
        [self.contentView addSubview:_label];
        [self.contentView addSubview:_checkBox];
    }
    return self;
}

-(void) creationComplete{
    [self changeCellWithImageWidth:_imageWidth imageHeight:_imageHeight titleHeight:_titleHeight];
    
}

-(void)changeCellWithImageWidth:(CGFloat)imageWidth imageHeight:(CGFloat)imageHeight titleHeight:(CGFloat)titleHeight{
    //_icon 和 _label的间距
    int space = 5;
    _icon.frame = CGRectMake((_icon.superview.frame.size.width - imageWidth)/2, (_icon.superview.frame.size.height - imageHeight - space - titleHeight)/2, imageWidth, imageHeight);
    _label.frame = CGRectMake(0, _icon.frame.origin.y +_icon.frame.size.height+space, _label.superview.frame.size.width, titleHeight);
    NSInteger checkBoxY = _icon.frame.origin.y - ceil((_checkBox.frame.size.height - _checkBox.textLabel.frame.size.height)/2.0);
    //    _checkBox.frame = CGRectMake(_icon.frame.origin.x + _icon.frame.size.width - imageWidth/3, checkBoxY, imageWidth/3, imageHeight/3);
    _checkBox.frame = CGRectMake(_icon.frame.origin.x + _icon.frame.size.width - imageWidth/3, checkBoxY, imageWidth/3, imageHeight/3);
}

//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//    _icon.layer.shadowColor = [UIColor yellowColor].CGColor;
//    _icon.layer.shadowOffset = CGSizeMake(0.5, 0.5);
//    _icon.layer.shadowOpacity = 0.5;
//    _icon.layer.shadowRadius = 6.0;
//    return YES;
//}

-(void)iconTouchDown{
    _icon.layer.shadowColor = [UIColor yellowColor].CGColor;
    _icon.layer.shadowOffset = CGSizeMake(0.5, 0.5);
    _icon.layer.shadowOpacity = 0.5;
    _icon.layer.shadowRadius = 6.0;
}

-(void)iconTouchUp{
    _icon.layer.shadowOpacity = 0;
}


-(void)setImagePath:(NSString *)aImagePath{
    _imagePath = aImagePath;
    if(![_imagePath isEqualToString:@""]){
        NSString *path = [[EnvironmentVariable getVersionPath] stringByAppendingString:@"/Image"];
        _image = [[UIImage alloc]initWithData:[FileUtil getFileDataWithPath:_imagePath andTypePath:path]];
        //        UIImage *iconImage = [UIImage imageWithContentsOfFile:_imagePath];
    }
    _icon.image = _image;
}

-(void)setImage:(UIImage *)image{
    _image = image;
    _icon.image = _image;
}

-(void)setTitle:(NSString *)aTitle{
    _title = aTitle;
    _label.text = _title;
}

-(void)setTitleColor:(UIColor *)titleColor{
    _titleColor = titleColor;
    _label.textColor = titleColor;
}

-(void)setTitleFontSize:(CGFloat)titleFontSize{
    _titleFontSize = titleFontSize;
    _label.font = [UIFont systemFontOfSize:titleFontSize];
}

-(void)setIsChecked:(BOOL)isChecked{
    _isChecked = isChecked;
    _checkBox.checked = isChecked;
}

@end
